home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / ENC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-19  |  3.2 KB  |  126 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/enc.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  */
  10.  
  11. #ifndef    lint
  12. static char rcsid_enc_c[] =
  13. "$Header: enc.c,v 4.6 88/11/15 11:28:31 jtkohl Exp $";
  14. #endif    lint
  15.  
  16. #include <mit_copy.h>
  17. #include <des.h>
  18. #ifdef BSDUNIX
  19. #include <sys/file.h>
  20. #endif
  21. #include <stdio.h>
  22.  
  23. des_key_schedule KEYSCHED;
  24. des_cblock key = {0,1,2,3,4,5,6,7};
  25. des_cblock sum;
  26. char inbuf[512+8];        /* leave room for cksum and len */
  27. char oubuf[512+8];
  28. int ind;
  29. int oud;
  30. long orig_size;
  31.  
  32. main(argc,argv)
  33.     int argc;
  34.     char *argv[];
  35. {
  36.     register int encrypt;
  37.     register long length;
  38.     register int *p;
  39.     long ivec[2];
  40.     if (argc != 4) {
  41.     fprintf (stderr, "%s: Usage: %s infile outfile mode.\n",
  42.          argv[0], argv[0]);
  43.     exit (1);
  44.     }
  45.     if (!strcmp(argv[3], "e"))
  46.     encrypt = 1;
  47.     else if (!strcmp(argv[3], "d"))
  48.     encrypt = 0;
  49.     else {
  50.     fprintf (stderr, "%s: Mode must be e (encrypt) or d (decrypt).\n",
  51.          argv[0]);
  52.     exit (1);
  53.     }
  54.     if ((ind = open(argv[1], O_RDONLY, 0666)) < 0) {
  55.     fprintf (stderr, "%s: Cannot open %s for input.\n",
  56.          argv[0], argv[1]);
  57.     exit (1);
  58.     }
  59.     if (!strcmp(argv[2], "-"))
  60.     oud = dup(1);
  61.     else if ((oud = open(argv[2], O_CREAT|O_WRONLY, 0666)) < 0) {
  62.     fprintf (stderr, "%s: Cannot open %s for output.\n",
  63.          argv[0], argv[2]);
  64.     exit (1);
  65.     }
  66. #ifdef notdef
  67.     (void) freopen ("/dev/tty", "r", stdin);
  68.     (void) freopen ("/dev/tty", "w", stdout);
  69. #endif
  70.     read_password (key, "\n\07\07Enter Key> ", 1);
  71.     if (des_key_sched (key, KEYSCHED) < 0) {
  72.     fprintf (stderr, "%s: Key parity error\n", argv[0]);
  73.     exit (1);
  74.     }
  75.     ivec[0] = 0;
  76.     ivec[1] = 0;
  77.     bcopy(key,sum,sizeof(des_cblock));
  78.     for (;;) {
  79.     if ((length = read (ind, inbuf, 512)) < 0) {
  80.         fprintf (stderr, "%s: Error reading from input.\n",
  81.              argv[0]);
  82.         exit (1);
  83.     } else if (length == 0) {
  84.         fprintf (stderr, "\n");
  85.         break;
  86.     }
  87.     if (encrypt) {
  88. #ifdef notdef
  89.         sum = des_quad_cksum(inbuf,NULL,length,1,sum);
  90. #endif
  91.         des_quad_cksum(inbuf,sum,length,1,sum);
  92.         orig_size += length;
  93.         fprintf(stderr,
  94.             "\nlength = %d tot length = %d quad_sum = %X %X",
  95.             length, orig_size, *(unsigned long *) sum,
  96.             *((unsigned long *) sum+1));
  97.         fflush(stderr);
  98.     }
  99.     des_pcbc_encrypt (inbuf, oubuf, (long) length, KEYSCHED, ivec,
  100.               encrypt);
  101.     if (!encrypt) {
  102. #ifdef notdef
  103.         sum = des_quad_cksum(oubuf,NULL,length,1,sum);
  104. #endif
  105.         des_quad_cksum(oubuf,sum,length,1,sum);
  106.         orig_size += length;
  107.         fprintf(stderr,
  108.             "\nlength = %d tot length = %d quad_sum = %X ",
  109.             length, orig_size, *(unsigned long *) sum,
  110.             *((unsigned long *) sum+1));
  111.     }
  112.     length = (length+7)& ~07;
  113.     write (oud, oubuf, length);
  114.     if (!encrypt)
  115.         p = (int *)&oubuf[length-8];
  116.     else
  117.         p = (int *)&inbuf[length-8];
  118.     ivec[0] = *p++;
  119.     ivec[1] = *p;
  120.     }
  121.  
  122.     fprintf(stderr,"\ntot length = %d quad_sum = %X\n",
  123.         orig_size,sum);
  124.     /* if encrypting, now put the original length and checksum in */
  125. }
  126.